<?php
class CampaignsController extends LiladsAppController {
	var $name = 'Campaigns';
	var $uses = array('Lilads.Campaign');

	function admin_stats() {
		$this->set('stats', true);
		$this->setAction('admin_index');
	}

	function admin_index() {
		$params = am($this->params['url'], $this->params['named']);
		if (isset($params['url'])) unset($params['url']);
		
		$this->set('campaigns', $this->paginate($this->Campaign->conditions($params)));
		$this->set('params', $params);
	}
	
	function admin_add() {
		$this->setAction('admin_edit');
	}

	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->Campaign->save($this->data)) {
				$this->Session->setFlash(__('Campaign has been saved.', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Please verify that the information is correct.', true), null, null, 'error');
			}
		}
		if (is_numeric($id)) {
			$this->data = $this->Campaign->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (is_numeric($id) && $this->Campaign->del($id)) {
			$this->Session->setFlash(sprintf(__('Campaign #%s has been deleted.', true), $id));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->cakeError('error404');
		}
	}

	function admin_activate($id = null) {
		if (is_numeric($id) && $this->Campaign->activate($id)) {
			$this->Session->setFlash(sprintf(__('Campaign #%s has been activated.', true), $id));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->cakeError('error404');
		}
	}

	function admin_refresh($id = null) {
		if (is_numeric($id) && $this->Campaign->refresh($id)) {
			$this->Session->setFlash(sprintf(__('Campaign #%s stats have been updated.', true), $id));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->cakeError('error404');
		}
		
	}
}
?>